/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.layers;

import java.util.Observable;
import java.util.Observer;

import android.graphics.Canvas;
import android.graphics.Rect;

import id.duapuluhdua.dakidaki.Command;
import id.duapuluhdua.dakidaki.R;
import id.duapuluhdua.dakidaki.Utils;
import id.duapuluhdua.dakidaki.common.CommonActivity;
import id.duapuluhdua.dakidaki.map.Screen;

/**
 *
 */
public class LayerInfo extends Layer implements Observer
{
  /** Fields */
  private TipFrameText mTipFrameLocation;
  private TipFrameAltitudeGraph mTipFrameAltGraph;

  private String mLocation;

  /**
   * The constructor.
   *
   * @param parent
   * @param screen
   */
  public LayerInfo(CommonActivity parent, Screen screen)
  {
    super(parent, screen);

    mTipFrameLocation = new TipFrameText(parent, mState,
        parent.getString(R.string.info_mode_frame_location));
    mTipFrameAltGraph = new TipFrameAltitudeGraph(parent, mState,
        parent.getString(R.string.info_mode_frame_altgraph));

    // Observe location status
    super.startObserveLocation(this);
  }

  @Override
  public void initData()
  {
    // Do nothing.
  }

  @Override
  public void surfaceSizeChanged(int width, int height, int screenOrientation)
  {
    mTipFrameLocation.setPosition(40);
    mTipFrameLocation.setMargins(40, 40);

    mTipFrameAltGraph.setPosition(mTipFrameLocation.getTitleBottom() + 40);
    mTipFrameAltGraph.setMargins(40, 40);
  }

  @Override
  public void updateObjectsState()
  {
    // Do nothing.
  }

  @Override
  public void doDraw(Canvas canvas, Rect area)
  {
    if (mState.getCurrentUiMode() == Command.CMD_MODE_INFO) {
      mTipFrameLocation.setText(mLocation);
      mTipFrameLocation.doDraw(canvas, area);
      mTipFrameAltGraph.doDraw(canvas, area);
    }
  }

  public void update(Observable observable, Object data)
  {
    if (mState.getCurrentUiMode() == Command.CMD_MODE_TRACKS) {
      super.updateLocation(observable, data);
    }
  }

  @Override
  public void updateLocation()
  {
    mLocation = "";
    synchronized (mLocationGeoPoint)
    {
      if (isLocationEnabled()) {
        String sLon = Utils.getLongitudeAsString(mLocationGeoPoint.wgsLon);
        String sLat = Utils.getLatitudeAsString(mLocationGeoPoint.wgsLat);
        mLocation = sLat + "\n" + sLon;
      }
    }
  }
}
